home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9374 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: NEwbie: How to return a multi-dimensional array from function?
  5. Date: 9 Mar 1996 18:18:39 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4ht3kf$vg@umbc9.umbc.edu>
  8. References: <4hp273$8bu@news.xs4all.nl> <31404CE9.1A4A@mc.net>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Anthony Moendir wrote:
  13. >> I want to return a multidimensional array fro a function,
  14.  
  15. Sean Park  <spark@mc.net> wrote:
  16. > You could pass the pointer to the array to the function.  That way the 
  17. > function itself will fill your array addresses rather than returning 
  18. > redundant data.  You might try:
  19. > void foo(char *tmp);
  20. > int main()
  21. > {
  22. >    char tmp[10][5];
  23. >    foo(tmp);
  24. >    return 0;
  25. > }
  26. > void foo(char *tmp)
  27. > {
  28. > /* You can reference tmp within this function as an array of what ever 
  29. >  * dimensions you want. */
  30. > /* do something */
  31. >  return;
  32. > }
  33.  
  34. How is passing a 2D array of char, pretending to be a pointer to char,
  35. to a function and making changes what the original poster asked for?
  36. I would prototype 'foo' as either:
  37.  
  38. void foo (char *tmp[5]);
  39. void foo (char tmp[][5]);
  40. void foo (char tmp[10][5]);
  41.  
  42. And then use it as a 2D array as would be expected from the way it was
  43. declared in main().
  44. -- 
  45. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  46.  
  47. Jonas J. Schlein  (schlein@gl.umbc.edu)
  48.